package com.kuaiying.kuai.ying.controller.admin;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.kuaiying.kuai.ying.model.admin.AdminModel;
import com.kuaiying.kuai.ying.model.leaflet.LeafletModel;
import com.kuaiying.kuai.ying.model.user.UserModel;
import com.kuaiying.kuai.ying.validator.HomeValidator;
import com.kuaiying.kuai.ying.validator.LoginValidator;
import com.kuaiying.kuai.ying.validator.RegistValidator;
import com.kuaiying.kuai.ying.validator.UploadValidator;

public class AdminController extends Controller{

	public void index(){
		String uName = getSessionAttr("username");
		if( uName != null ){
			redirect("/admin/home");
			return;
		}
		//redirect("/admin/home");
		render("login.html");
	}
	
	public void login(){
		String uName = getSessionAttr("username");
		if( uName != null ){
			System.out.println( "has username :" + uName );
			redirect("home");
			return;
		}
		render("login.html");
	}
	
	@Before( LoginValidator.class )
	public void loginPost(){
		
		String userName = getPara("name");
    	String pwd = getPara("password");
    	System.out.println( userName );
    	AdminModel user = AdminModel.me.getUserByNameAndPwd(userName, pwd);
    	//redirect("/admin/home");
    	if (user != null){
            String bbsID = userName + pwd;
            setCookie("bbsID", bbsID, 3600*24*30);
            setSessionAttr("username", userName);
            setSessionAttr("userID", user.get("user_id"));
            //List<AdminModel> list = AdminModel.me.find("select * from admin");
            //System.out.println( list.toString() );
            //setAttr("allUserList", list);
            //redirect("/admin/home",list);
            List<LeafletModel> list = LeafletModel.me.getAllLeaftel();
            //System.out.println( list.toString() );
            List allShowList=new ArrayList();
            for(Iterator i = list.iterator();i.hasNext(); ){
            	LeafletModel leafletmodel = (LeafletModel)i.next();
            	List items = new ArrayList();
            	items.add(leafletmodel.getInt("leaflet_id"));
            	items.add(leafletmodel.getStr("school"));
            	items.add(leafletmodel.getInt("shop_id"));
            	items.add(leafletmodel.getInt("user_num"));
            	items.add(leafletmodel.getInt("use_gold"));
            	items.add(leafletmodel.getInt("money"));
            	items.add(leafletmodel.getInt("gold_num"));
            	items.add(leafletmodel.getInt("remainder_money"));
            	allShowList.add(items);
            }
            //System.out.println(str);
            setAttr("allShowList", allShowList);
            List allShowTitle=new ArrayList();
            allShowTitle.add("leafletid");
            allShowTitle.add("school");
            allShowTitle.add("shop_id");
            allShowTitle.add("user_num");
            allShowTitle.add("use_gold");
            allShowTitle.add("money");
            allShowTitle.add("gold_num");
            allShowTitle.add("remainder_money");
            setAttr("allShowTitle", allShowTitle);
            //redirect("/admin/home",list);
            render("home.html");
            //render("home.html");
        }else{
            setAttr("loginError", "用户名或密码错误");
            render("login.html");
        }
	}
	
	@Before( HomeValidator.class )
	public void home(){
		render("home.html");
	}
	
	public void quit(){
		removeSessionAttr("username");
		removeSessionAttr("userID");
		removeCookie("bbsID");
		render("login.html");
	}

	public void upload(){
		UploadFile file = getFile("UpLoadFile");
		render("upload.html");	
	}
	
	public void download() {
	    renderFile(new File("./2011年计算机文化基础试题.pdf"));
	}
	
	//展示传单信息
	public void showLeaflet(){
		String uName = getSessionAttr("username");
		if( uName != null ){
            List<LeafletModel> list = LeafletModel.me.getAllLeaftel();
            //System.out.println( list.toString() );
            List allShowList=new ArrayList();
            for(Iterator i = list.iterator();i.hasNext(); ){
            	LeafletModel leafletmodel = (LeafletModel)i.next();
            	List items = new ArrayList();
            	items.add(leafletmodel.getStr("leaflet_id"));
            	items.add(leafletmodel.getStr("school"));
            	items.add(leafletmodel.getStr("shop_id"));
            	items.add(leafletmodel.getStr("user_num"));
            	items.add(leafletmodel.getStr("use_gold"));
            	items.add(leafletmodel.getStr("money"));
            	items.add(leafletmodel.getStr("gold_num"));
            	items.add(leafletmodel.getStr("remainder_money"));
            	allShowList.add(items);
            }
            //System.out.println(str);
            setAttr("allShowList", allShowList);
            List allShowTitle=new ArrayList();
            allShowTitle.add("leafletid");
            allShowTitle.add("school");
            allShowTitle.add("shop_id");
            allShowTitle.add("user_num");
            allShowTitle.add("use_gold");
            allShowTitle.add("money");
            allShowTitle.add("gold_num");
            allShowTitle.add("remainder_money");
            setAttr("allShowTitle", allShowTitle);
            //redirect("/admin/home",list);
            render("home.html");
		}
	}
	
}
